package com.ybServer.controller;

import com.ybServer.model.BaseResponse;
import com.ybServer.model.ResultCode;
import com.ybServer.model.User;
import com.ybServer.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

@Controller
@RequestMapping("/user")

public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("showUser")
    @ResponseBody
    public HashMap<String, Object> selectUser(HttpServletRequest request) throws IOException {

        User[] users = this.userService.selectUserByName(request.getParameter("name"));

        HashMap<String, Object> result = new HashMap<>();
        result.put("users", users);
        return result;
    }


    @RequestMapping("userLogin")
    @ResponseBody
    public BaseResponse<User> userLogin(@RequestParam(value = "account", required = false) String account,
                                        @RequestParam("password") String password,
                                        HttpSession session)
            throws Exception {
        BaseResponse<User> response;
        User user = new User();
        if (account.contains("@"))
            user.setEmail(account);
        else
            user.setPhone(account);

        user.setPassword(password);

        User u = userService.userLogin(user);
        if (u != null) {
            session.setAttribute("user", u);
            response = new BaseResponse<>(ResultCode.SUCCESS, "loginSuccess");
            response.setData(u);
        } else {
            response = new BaseResponse<>(ResultCode.FAIL, "loginFailed");
        }
        return response;
    }

    @RequestMapping("register")
    @ResponseBody
    public BaseResponse<User> register(@RequestParam("account") String account,
                                       @RequestParam("password") String password,
                                       @RequestParam("name") String name) throws Exception {
        BaseResponse<User> baseResponse;
        User user = new User();
        if (account.contains("@"))
            user.setEmail(account);
        else
            user.setPhone(account);
        user.setName(name);
        user.setPassword(password);
        if (userService.userExists(user)) {
            baseResponse = new BaseResponse<>(ResultCode.FAIL, "该邮箱或手机号已注册");
        } else {
            try {
                userService.userRegister(user);
                baseResponse = new BaseResponse<>(ResultCode.SUCCESS, "registerSuccess");
                baseResponse.setData(user);
                System.out.println("id:" + user.getId());
            } catch (Exception e) {
                baseResponse = new BaseResponse<>(ResultCode.EXCEPTION, "exception");
            }
        }
        return baseResponse;
    }

    @RequestMapping("queryUser")
    @ResponseBody
    public BaseResponse queryUser(@RequestParam("email") String email,
                                  @RequestParam("phone") String phone) throws Exception {
        BaseResponse baseResponse;
        User user = new User();
        user.setEmail(email);
        user.setPhone(phone);

        if (userService.userExists(user))
            baseResponse = new BaseResponse(ResultCode.FAIL, "exist");
        else
            baseResponse = new BaseResponse(ResultCode.SUCCESS, "usable");
        return baseResponse;
    }

    @RequestMapping("getUserIcon")
    public void getUserIcon(@RequestParam("userId") String userId, HttpServletResponse response) throws Exception {
        byte[] bytes = userService.getUserIconByUserId(userId);
        response.setContentType("image/*");
        OutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }

    @RequestMapping("getUserBasicInfoById")
    @ResponseBody
    public BaseResponse<User> getUserBasicInfoById(@RequestParam("userId") long userId) throws Exception {
        BaseResponse<User> baseResponse = new BaseResponse<>();
        try {
            User user = userService.getUserBasicInfoById(userId);
            baseResponse.setData(user);
            baseResponse.setStatus(ResultCode.SUCCESS);
        } catch (Exception e) {
            baseResponse.setStatus(ResultCode.EXCEPTION);
        }
        return baseResponse;
    }

    @RequestMapping("submitFeedback")
    @ResponseBody
    public BaseResponse submitFeedback(@RequestParam(value = "name", required = false) String name,
                                       @RequestParam(value = "phone", required = false) String phone,
                                       @RequestParam("content") String content) {
        System.out.println(name + phone + content);
        BaseResponse baseResponse = new BaseResponse();
        try {
            userService.addFeedback(name, phone, content);
            baseResponse.setStatus(ResultCode.SUCCESS);
        } catch (Exception e) {
            baseResponse.setStatus(ResultCode.EXCEPTION);
            baseResponse.setMessage(e.getMessage());
        }
        return baseResponse;
    }
    @RequestMapping("addUserIcon")
    @ResponseBody
    public BaseResponse updateUserIcon(@RequestParam("userIcon") MultipartFile pictures) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            userService.updateUserIcon(pictures);
            baseResponse.setStatus(ResultCode.SUCCESS);
        }catch (Exception e){
            baseResponse.setStatus(ResultCode.EXCEPTION);
            baseResponse.setMessage(e.toString());
        }
        return baseResponse;
    }

}